<template>
	<a-card :bordered="false" style="margin-bottom: 10px">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item name="name" label="物料名称">
						<a-input v-model:value="searchFormState.name" placeholder="请输入物料名称" />
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-button type="primary" @click="tableRef.refresh(true)">
						<template #icon><SearchOutlined /></template>
						查询
					</a-button>
					<a-button class="snowy-buttom-left" @click="reset">
						<template #icon><redo-outlined /></template>
						重置
					</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table ref="tableRef" :columns="columns" :data="loadData" bordered :scroll="{ x: 700 }" :tool-config="toolConfig">
			<template #operator class="table-operator">
				<div style="display: flex; justify-content: right">
					<a-select :getPopupContainer="trigger => trigger.parentNode" ref="select" v-model:value="value1" v-show="false" style="width: 120px">
						<a-select-option value="本机构">本机构</a-select-option>
						<a-select-option value="全联盟">全联盟</a-select-option>
					</a-select>

					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
				</div>
			</template>
			<template #bodyCell="{ column, record, index }">
				<template v-if="column.dataIndex === 'index'">
					<span>{{ index + 1 }}</span>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="formRef.onOpen(record)">编辑</a>
					<a-divider type="vertical" />
					<a-popconfirm title="是否删除?" @confirm="remove(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh(true)" />
</template>

<script setup name="promaterial">
	import promaterialApi from '@/api/base_data/promaterialApi'
	import Form from './form.vue'
	const columns = [
		{
			title: '序号',
			dataIndex: 'index',
			ellipsis: true,
			width: '50px',
			align: 'center'
		},
		{
			title: '物料编号',
			dataIndex: 'code',
			width: '120px',
			align: 'center',
			checked: true
		},
		{
			title: '物料名称',
			dataIndex: 'name',
			width: '120px',
			align: 'center'
		},
		{
			title: '规格型号',
			dataIndex: 'spec',
			ellipsis: true,
			align: 'center',
			width: '120px'
		},
		{
			title: '品牌',
			dataIndex: 'brand',
			align: 'center',
			width: '120px'
		},
		{
			title: '单位',
			dataIndex: 'unit',
			align: 'center',
			width: '80px'
		},
		{
			title: '人工单价(元)',
			dataIndex: 'laborUnitPrice',
			align: 'center',
			width: '100px'
		},
		{
			title: '成本单价(元)',
			dataIndex: 'costUnitPrice',
			align: 'center',
			width: '100px'
		},
		{
			title: '综合单价(元)',
			dataIndex: 'synUnitPrice',
			align: 'center',
			width: '100px'
		},
		{
			title: '物料单价(元)',
			dataIndex: 'unitPrice',
			align: 'center',
			width: '100px'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '120px',
			fixed: 'right'
		}
	]

	let value1 = ref('本机构')
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 定义tableDOM
	const tableRef = ref(null)
	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		return promaterialApi
			.getPage(Object.assign(parameter, searchFormState.value))
			.then((data) => {
				return data
			})
			.catch(() => {})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		promaterialApi.delPromaterial(params).then(() => {
			tableRef.value.refresh()
		})
	}
</script>

<style scoped>
	.ant-form-item {
		margin-bottom: 0 !important;
	}
	.primaryAdd {
		margin-right: 10px;
	}
	.snowy-buttom-left {
		margin-left: 8px;
	}
</style>
